package com.acwer.oa.web;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.attribute.FileAttribute;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.TimeZone;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import com.fasterxml.jackson.databind.ObjectMapper;

@Configuration
public class OAWebMvcConfigurer implements WebMvcConfigurer {

	@Autowired
	private JwtInterceptor jwtInterceptor;

	/** 默认日期时间格式 */
	  public static final String DEFAULT_DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
	  /** 默认日期格式 */
	  public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";
	  /** 默认时间格式 */
	  public static final String DEFAULT_TIME_FORMAT = "HH:mm:ss";
	  
	public void addInterceptors(InterceptorRegistry registry) {
		// 注册拦截器要声明拦截器对象和要拦截的请求
		registry.addInterceptor(jwtInterceptor).addPathPatterns("/**").excludePathPatterns("/login");
	}
	/**
	 * 解决ie responsebody返回json的时候提示下载问题
	 * 
	 * @return
	 */
	public MappingJackson2HttpMessageConverter customJackson2HttpMessageConverter() {
		MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter();
		ObjectMapper mapper = jsonConverter.getObjectMapper();
		System.out.println("===MappingJackson2HttpMessageConverter");
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SS");
		format.setTimeZone(TimeZone.getTimeZone("GMT+:08:00"));
		// System.out.println(TimeZone.getTimeZone("GMT+:08:00"));
		mapper.setDateFormat(format);
		jsonConverter.setObjectMapper(mapper);
		List<MediaType> supportedMediaTypes = new ArrayList<MediaType>();
		MediaType media = new MediaType(MediaType.TEXT_HTML, Charset.forName("UTF-8"));
		supportedMediaTypes.add(media);
		jsonConverter.setSupportedMediaTypes(supportedMediaTypes);
		return jsonConverter;
	}

	/**
	 * 加入文件上传路径
	 */
	@Value("${spring.activiti.attach-path}")
	private String attachPath;
	@Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //配置静态资源处理
		if(Files.notExists(Paths.get(attachPath))){
			try {
				
				File dir=Files.createDirectories(Paths.get(attachPath)).toFile();
				dir.setReadable(true);
				dir.setWritable(true);
				dir.setExecutable(true);
				
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		registry.addResourceHandler("/**")
        .addResourceLocations("file:"+attachPath);
    }
	@Override
	public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
		converters.add(customJackson2HttpMessageConverter());
	}

	public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
		configurer.enable();
	}
	

}